package com.yss.kettle.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.yss.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yss.kettle.service.IJobDatasourceService;
import com.yss.kettle.mapper.JobDatasourceMapper;
import com.yss.kettle.domain.JobDatasource;

/**
 * ETL数据源信息Service业务层处理
 *
 * @author yss
 * @date 2023-04-23
 */
@Slf4j
@Service
public class JobDatasourceServiceImpl extends ServiceImpl<JobDatasourceMapper, JobDatasource> implements IJobDatasourceService {
    /**
     * 查询ETL数据源信息列表
     *
     * @param jobDatasource ETL数据源信息
     * @return ETL数据源信息
     */
    @Override
    public List<JobDatasource> selectJobDatasourceList(JobDatasource jobDatasource) {
        QueryWrapper<JobDatasource> wrapper = new QueryWrapper();
        if (StringUtils.isNull(jobDatasource)) {
            return this.list(wrapper);
        }
        if (StringUtils.isNotEmpty(jobDatasource.getName())) {
            wrapper.like("NAME", jobDatasource.getName());
        }
        if (StringUtils.isNotEmpty(jobDatasource.getFtype())) {
            wrapper.eq("FTYPE", jobDatasource.getFtype());
        }
        if (null != jobDatasource.getStatus()) {
            wrapper.eq("STATUS", jobDatasource.getStatus());
        }
        return this.list(wrapper);
    }


    /**
     * 校验名称是否唯一
     *
     * @param jobDatasource
     * @return 结果
     */
    @Override
    public boolean checkJobDatasourceNameUnique(JobDatasource jobDatasource) {
        String tableId = StringUtils.isNull(jobDatasource.getId()) ? "-1" : jobDatasource.getId();
        QueryWrapper<JobDatasource> wrapper = new QueryWrapper<>();
        wrapper.eq("NAME", jobDatasource.getName());
        JobDatasource info = this.getOne(wrapper);
        if (StringUtils.isNotNull(info) && !info.getId().equals(tableId)) {
            return false;
        }
        return true;
    }

}
